import * as user from '@/api/user';
import * as types from '../action-types';
import router from '@/router';
import { setLocal, getLocal } from '@/utils/local';;//token存储在本地会话中
import per from '@/router/per.js';


const filterRouter = (authList) => {//过滤路由的方法
  console.log(authList)//后端返回的数据中auth属性是进行权限鉴定的 
  let auths = authList.map(auth=>auth.auth);//获取到auths的名称进行筛选

  console.log(auths, 'auths');
  function filter(routes) {
    return routes.filter(route => {
      if(auths.includes(route.meta.auth)){//auths匹配上的就会有相对应的菜单
        if (route.children) { //如果有娃儿的话就继续匹配
          route.children = filter(route.children)
        }
        return route;
      }
    })
  }
  return filter(per);
}

// 进行判断是否添加过路由，所以在src/store/modules/user.js里进行添加

export default {
  state: {
    userInfo: {}, // 用户信息
    hasPermission: false, // 代表用户权限
    menuPermission: false,//代表着菜单权限
  },

  mutations: {
    [types.SET_USER](state, userInfo) {
      console.log(state, userInfo, 'state, userInfo')
      state.userInfo = userInfo;
      if (userInfo && userInfo.token) {//如果有token值就存储在本地会话中
        setLocal('token', userInfo.token);
      } else {
        localStorage.clear('token');
      }
    },
    //  代表用户权限
    [types.SET_PERMISSION](state, has) {
      state.hasPermission = has;
    },
    //代表着菜单权限
    [types.SET_MENU_PERMISSION](state, has) {
      state.menuPermission = has;
  }
  },
  actions: {
    async [types.SET_USER]({ commit }, { payload, permission }) {
      console.log(payload, permission, ' payload, permission=>actions')
      commit(types.SET_USER, payload);
      commit(types.SET_PERMISSION, permission)
    },
    async [types.USER_LOGIN]({ commit, dispatch }, payload) {
      try {
        let result = await user.login(payload);
        console.log(result, 'result=>USER_LOGIN')
        dispatch(types.SET_USER, { payload: result.data, permission: true })
      } catch (e) {
        return Promise.reject(e);
      }
    },


    async [types.USER_VALIDATE]({ dispatch }) {
      // 如果没token 就不用发请求了 肯定没登录
      if (!getLocal('token')) return false;
      try {
        let result = await user.validate();
        dispatch(types.SET_USER, { payload: result.data, permission: true })
        return true;
      } catch (e) {
        dispatch(types.SET_USER, { payload: {}, permission: false });
        return false;
      }
    },

    async [types.USER_LOGOUT]({ dispatch }) {
      dispatch(types.SET_USER, { payload: {}, permission: false })
    },
    async [types.ADD_ROUTE]({ commit, state }) {
         // 后端返回的用户的权限
         let authList = state.userInfo.authList; 
      console.log(authList, per, 'authList,per')//authList代表着后端返回的数据，per是前端设置的路径
      if (authList) { // 通过权限过滤出当前用户的路由--不同的用户有不同的菜单
        let routes = filterRouter(authList);
        // 找到manager路由作为第一级 如/manager/articleManager.vue
        //在路由的配置信息上进行查找 --- manager这一条
        console.log(router,'router',routes,'filterRouter=>routes');
        let route = router.options.routes.find(item => item.path === '/manager');
        console.log(route,'route是谁？？')
        route.children = routes; // 给manager添加儿子路由
        console.log(route, 'route');
        router.addRoutes([route]); // 动态添加进去
        commit(types.SET_MENU_PERMISSION, true); // 权限设置完毕
      } else {
        commit(types.SET_MENU_PERMISSION, true); // 权限设置完毕
      }
    }
  }

}